package com.genghis.rapidcrm.web.view;

import com.genghis.rapidcrm.kernel.UsersRecordModel;
import com.genghis.rapidcrm.kernel.module.RapidModuleModel;
import com.genghis.rapidcrm.web.view.menu.RapidMenuModel;
import com.genghis.rapidcrm.web.view.menu.RapidMenuModelProvider;
import com.genghis.rapidcrm.web.view.menu.RapidMenuStructureModel;
import com.genghis.rapidcrm.web.view.model.CompanyLogo;
import com.genghis.rapidcrm.web.view.model.RapidCssScriptModel;
import com.genghis.rapidcrm.web.view.model.RapidJsScriptModel;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author xulixin
 */
public abstract class RapidBasicView extends RapidFooterView {

    @Override
    protected void preProcess(HttpServletRequest request, HttpServletResponse response, boolean display)
            throws Exception {

        super.preProcess(request, response, false);

        RapidViewer viewer = this.getViewer(request);

        List<RapidMenuModel> menuModelsList = getAppContextBean(RapidMenuModelProvider.class).getAll(true);
        String selectedModule = getModule(request);
        RapidMenuStructureModel menuStructure = RapidMenuStructureModel.getInstanceFromMenuList(menuModelsList, selectedModule);

//        RapidCompanyDetails companyDetails = getAppContextBean(CompanyManager.class).getById();
//        String companyLogo = companyDetails.getLogo();
        CompanyLogo companyLogo = new CompanyLogo();
        companyLogo.setImagePath("/test/logo/rapid-crm-logo.png");
        companyLogo.setAlt("RapidCRM");
        companyLogo.setTitle("Genghis");
        viewer.assign("CURRENTDATE", System.currentTimeMillis());
        viewer.assign("MODULE", selectedModule);
        viewer.assign("MODULE_NAME", selectedModule);
        viewer.assign("QUALIFIED_MODULE", selectedModule);
        viewer.assign("PARENT_MODULE", request.getParameter("parent"));
        viewer.assign("VIEW", request.getParameter("view"));

        // Order by pre-defined automation process for QuickCreate.
        RapidMenuStructureModel.sortMenuItemsByProcess(menuModelsList);

        viewer.assign("MENUS", menuModelsList);
        viewer.assign("MENU_STRUCTURE", menuStructure);
        viewer.assign("MENU_SELECTED_MODULENAME", selectedModule);
        viewer.assign("MENU_TOPITEMS_LIMIT", menuStructure.getLimit());
        viewer.assign("COMPANY_LOGO", companyLogo);
        viewer.assign("USER_MODEL", UsersRecordModel.getCurrentUserModel(request));

        RapidModuleModel homeModuleModel = getModuleModelInstance("Home");
        viewer.assign("HOME_MODULE_MODEL", homeModuleModel);
        viewer.assign("HEADER_LINKS", this.getHeaderLinks(request));
        viewer.assign("ANNOUNCEMENT", this.getAnnouncement());
        viewer.assign("SEARCHABLE_MODULES", homeModuleModel.getSearchableModules());

        if (display) {
            this.preProcessDisplay(request, response);
        }
    }

    @Override
    protected String preProcessTplName(HttpServletRequest request) {
        return "BasicHeader.tpl";
    }

    //Note: To get the right hook for immediate parent in PHP,
    // specially in case of deep hierarchy
	/*function preProcessParentTplName(Vtiger_Request $request) {
		return parent::preProcessTplName($request);
	}*/

    @Override
    protected void postProcess(HttpServletRequest request, HttpServletResponse response) throws Exception {
//        RapidViewer viewer = this.getViewer(request);
        //viewer.assign("GUIDERSJSON", Vtiger_Guider_Model::toJsonList($this->getGuiderModels($request)));
        super.postProcess(request, response);
    }

    /**
     * Function to get the list of Script models to be included
     *
     * @param request HttpServletRequest
     * @return <Array> - List of Vtiger_JsScript_Model instances
     */
    @Override
    protected List<RapidJsScriptModel> getHeaderScripts(HttpServletRequest request) {
        List<RapidJsScriptModel> headerScriptInstances = super.getHeaderScripts(request);
        String moduleName = getModule(request);

        String[] jsFileNames = {
                "libraries.bootstrap.js.eternicode-bootstrap-datepicker.js.bootstrap-datepicker",
                "~libraries/bootstrap/js/eternicode-bootstrap-datepicker/js/locales/bootstrap-datepicker." +
                        getLanguageHandler(request).getShortLanguageName() + ".js",
                "~libraries/jquery/timepicker/jquery.timepicker.min.js",
                "modules.Vtiger.resources.Header",
                "modules.Vtiger.resources.Edit",
                String.format("modules.%s.resources.Edit", moduleName),
                "modules.Vtiger.resources.Popup",
                String.format("modules.%s.resources.Popup", moduleName),
                "modules.Vtiger.resources.Field",
                String.format("modules.%s.resources.Field", moduleName),
                "modules.Vtiger.resources.validator.BaseValidator",
                "modules.Vtiger.resources.validator.FieldValidator",
                String.format("modules.%s.resources.validator.FieldValidator", moduleName),
                "libraries.jquery.jquery_windowmsg",
                "modules.Vtiger.resources.BasicSearch",
                String.format("modules.%s.resources.BasicSearch", moduleName),
                "modules.Vtiger.resources.AdvanceFilter",
                String.format("modules.%s.resources.AdvanceFilter", moduleName),
                "modules.Vtiger.resources.SearchAdvanceFilter",
                String.format("modules.%s.resources.SearchAdvanceFilter", moduleName),
                "modules.Vtiger.resources.AdvanceSearch",
                String.format("modules.%s.resources.AdvanceSearch", moduleName)};

        Map<String, RapidJsScriptModel> jsScriptInstances = this.checkAndConvertJsScripts(jsFileNames);
        headerScriptInstances.addAll(jsScriptInstances.values());
        return headerScriptInstances;
    }

    @Override
    public List<RapidCssScriptModel> getHeaderCss(HttpServletRequest request) {
        List<RapidCssScriptModel> headerCssInstances = super.getHeaderCss(request);

        String[] cssFileNames = {"~/libraries/jquery/timepicker/jquery.timepicker.css"};
        List<RapidCssScriptModel> cssInstances = this.checkAndConvertCssStyles(cssFileNames, request);
        headerCssInstances.addAll(cssInstances);
        return headerCssInstances;
    }

    List<?> getGuiderModels(HttpServletRequest request) {
        return new ArrayList<>(0);
    }
}
